<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 校园表白墙</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/css/style.css">
    <style>
        .login-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .login-card {
            width: 100%;
            max-width: 450px;
            padding: 40px;
            background: white;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-header h1 {
            color: var(--primary-color);
            font-size: 2rem;
            margin-bottom: 10px;
        }
        .login-header p {
            color: var(--text-secondary);
            font-size: 0.9rem;
        }
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(255, 107, 157, 0.25);
        }
        .tab-buttons {
            display: flex;
            margin-bottom: 30px;
        }
        .tab-btn {
            flex: 1;
            padding: 12px;
            border: none;
            background: #f5f5f5;
            color: var(--text-secondary);
            cursor: pointer;
            transition: all 0.3s;
        }
        .tab-btn:first-child {
            border-radius: 10px 0 0 10px;
        }
        .tab-btn:last-child {
            border-radius: 0 10px 10px 0;
        }
        .tab-btn.active {
            background: var(--primary-color);
            color: white;
        }
        .form-section {
            display: none;
        }
        .form-section.active {
            display: block;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <h1><i class="bi bi-heart-fill"></i> 校园表白墙</h1>
                <p>分享你的心声，记录校园时光</p>
            </div>
            
            <div class="tab-buttons">
                <button class="tab-btn active" onclick="switchTab('login')">登录</button>
                <button class="tab-btn" onclick="switchTab('register')">注册</button>
            </div>
            
            <div id="loginForm" class="form-section active">
                <form onsubmit="handleLogin(event)">
                    <div class="mb-3">
                        <label class="form-label">学号/手机号</label>
                        <input type="text" class="form-control" id="loginAccount" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">密码</label>
                        <input type="password" class="form-control" id="loginPassword" required>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-2">登录</button>
                </form>
            </div>
            
            <div id="registerForm" class="form-section">
                <form onsubmit="handleRegister(event)">
                    <div class="mb-3">
                        <label class="form-label">学号</label>
                        <input type="text" class="form-control" id="regStudentId" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">真实姓名</label>
                        <input type="text" class="form-control" id="regRealName" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">手机号</label>
                        <input type="tel" class="form-control" id="regPhone" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">密码</label>
                        <input type="password" class="form-control" id="regPassword" required minlength="6">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">学校</label>
                        <input type="text" class="form-control" id="regSchool" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">学院</label>
                        <input type="text" class="form-control" id="regCollege" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">年级</label>
                        <input type="number" class="form-control" id="regGrade" required min="2020" max="2030">
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-2">注册</button>
                </form>
            </div>
            
            <div class="text-center mt-4">
                <a href="/admin-login.html" class="text-muted" style="font-size: 0.9rem;">管理员登录</a>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        function switchTab(tab) {
            $('.tab-btn').removeClass('active');
            $('.form-section').removeClass('active');
            
            if (tab === 'login') {
                $('.tab-btn:first').addClass('active');
                $('#loginForm').addClass('active');
            } else {
                $('.tab-btn:last').addClass('active');
                $('#registerForm').addClass('active');
            }
        }
        
        function handleLogin(event) {
            event.preventDefault();
            
            const account = $('#loginAccount').val();
            const password = $('#loginPassword').val();
            
            request('/auth/login', {
                method: 'POST',
                body: JSON.stringify({ account, password })
            })
            .then(data => {
                setToken(data.token);
                setUserInfo(data.userInfo);
                showToast('登录成功', 'success');
                setTimeout(() => {
                    window.location.href = '/home.html';
                }, 500);
            })
            .catch(err => {
                showToast(err.message, 'danger');
            });
        }
        
        function handleRegister(event) {
            event.preventDefault();
            
            const data = {
                studentId: $('#regStudentId').val(),
                realName: $('#regRealName').val(),
                phone: $('#regPhone').val(),
                password: $('#regPassword').val(),
                school: $('#regSchool').val(),
                college: $('#regCollege').val(),
                grade: parseInt($('#regGrade').val())
            };
            
            request('/auth/register', {
                method: 'POST',
                body: JSON.stringify(data)
            })
            .then(() => {
                showToast('注册成功，请登录', 'success');
                switchTab('login');
                $('#loginAccount').val(data.studentId);
            })
            .catch(err => {
                showToast(err.message, 'danger');
            });
        }
        
        $(document).ready(function() {
            const token = getToken();
            if (token) {
                window.location.href = '/home.html';
            }
        });
    </script>
</body>
</html>

